class Piece:
    def __init__(self, pos_number, color=None, cx=0, cy=0, radius=20):
        self.pos_number = pos_number
        self.color = color or (255, 255, 255)
        self.cx = cx
        self.cy = cy
        self.radius = radius

    # 画棋子
    def draw(self):
        pygame.draw.circle(screen, self.color, (self.cx, self.cy), self.radius)

    # 翻转棋子颜色
    def flip_color(self):
        if self.color == black:
            self.color = white
        else:
            self.color = black

    # 移动棋子
    def move(self, dest):
        global last_move

        if last_move is not None:
            # 将 last_move 的颜色翻转
            last_move.flip_color()

        # 移动棋子的方法
        self.x, self.y = POSITION[dest]
        last_move = self

    # 判断是否可以跳到目标棋子
    def can_jump(self, target_piece):
        # 如果有棋子，则不能跳
        if target_piece is not None:
            return False

        for adj in self.adjacent_pieces(self.pos_number):
            mid_piece = self.get_mid_piece(self.pos_number, adj)
            if mid_piece is None:
                continue
            dest = self.get_dest(mid_piece.pos_number, self.pos_number, adj)
            if target_piece == dest:
                return True
        return False

    # 判断棋子是否可以移动到某个位置
    def can_move_to(self, dest):
        # 如果目标已经有棋子，则不能移动
        if self.get_piece(dest) is not None:
            return False

        for adj in self.adjacent_pieces(self.pos_number):
            mid_piece = self.get_mid_piece(self.pos_number, adj)
            # 如果有棋子，则不能跳
            if mid_piece is None:
                continue
            _dest = self.get_dest(mid_piece.pos_number, self.pos_number, adj)
            if dest == _dest:
                return True
        return False

    @staticmethod
    def get_dest(mid, start, end):
        if mid == start:
            return end
        else:
            return start

    @staticmethod
    def adjacent_pieces(pos_number):
        ADJACENT = {
            1: [2, 9],
            2: [1, 3],
            3: [2, 4],
            4: [3, 5],
            5: [4, 6],
            6: [5, 7],
            7: [6, 8],
            8: [7, 9],
            9: [1, 10, 8],
            10: [9, 5],
        }
        return ADJACENT[pos_number]

    @staticmethod
    def get_mid_piece(start, end):
        mid = ((start + end) // 2)
        return get_piece(mid)

    @staticmethod
    def get_piece(pos_number):
        for piece in self.pieces:
            if piece is not None and piece.pos_number == pos_number:
                return piece
        return None